$(document).ready(function(){
    /* Video */
    $('.lienVideo').click(function() {
         var url = AJAX_PATH + "videoAssoc.ajax.php";
         var data = {id: $(this).attr('id')};
         $.ajax({type: "POST",
           url: url,
           data: data,
            beforeSend:function() {
                $("#videoAssoc").html("<table class='preload'><tr><td><img src='" + IMG_PATH + "icones/preloader.gif' alt='Chargement...' /></td></tr></table>");
            },
            success:function(data){
                $("#videoAssoc").html(data);
            }
        });
    });

    $.validator.addMethod('isSelected',function(){
        if($("#free_amount").is(':checked')){
            if(!isNaN($(this).val())) return false;
            else return true;
        }else return true;
    });
    $("#free_amount").click(function(){
        if($(this).is(':checked')){
            $("#free_amount_input").addClass("required");
        }
    });
    $(".notfree").click(function(){
            $("#free_amount_input").removeClass("required");
            $("#free_amount_input").removeClass("error");
            $("#free_amount_input").next("label").hide();
            $("#realcost").html(Math.round(parseInt($(this).val())*340)/1000);
     });
     $("#free_amount_input").click(function(){
        $("#free_amount").attr("checked", "checked");
    });
     $("#free_amount_input").change(function(){
         if(!isNaN($(this).val()))
         $("#realcost").html(Math.round(parseInt($(this).val())*340)/1000);
     else $("#realcost").html('-');
     });
     $("#form_donation").validate({
        rules: {
            free_amount_input : {
                isSelected: true
            }
        },
        messages: {
            free_amount_input: "Un nombre est requis pour ce champ",
            accept: "Vous devez accepter les conditions"
        },errorPlacement: function(error, element) {
			if ( element.is(":checkbox") )
				error.appendTo ( element.parent() );
            
			else
				error.appendTo( element.parent() );
		},

        onkeyup: false
    });

    /* Soutiens */
    $(".addSupport").click(function() {
        var t = $(this);
        var user_id = session['user']['id'];
        var association_id = t.attr("id").split("_");
        var url = AJAX_PATH + "support.ajax.php";
        var data = {action: "add", user_id: user_id, association_id: association_id[1]};

        if(!user_id) {
            tb_init_login("ajax", url, data);
        } else {
            $.ajax({type: "POST",
               url: url,
               data: data,
                success:function(data){
                    if(data != "") {
                        alert("Vous ne pouvez pas supporter plus de trois associations");
                    } else {
                        t.parents(".don").html("<div class='elem2'><div class='image'><img src='" + IMG_PATH + "icones/soutenu.png' /></div><div class='txt'><br></div></div>");
                        location.reload();
                    }
                }
            });
        }
    });
    $(".delSupport").click(function() {
        var t = $(this);
        var user_id = session['user']['id'];
        var association_id = t.attr("id").split("_");
        var url = AJAX_PATH + "support.ajax.php";
        var data = {action: "del", user_id: user_id, association_id: association_id[1]};

        $.ajax({type: "POST",
           url: url,
           data: data,
            success:function(){
                t.parents(".assoc_soutenu").slideUp("fast");
                location.reload();
            }
        });
    });

    /* Commentaires */
    $("#post").click(function(){
        $("#error-com").slideUp("fast");
        var comment = trim($(".add_comment").val());
        var nbcomments = parseInt($("#nbcomments").html());
        var id_user = session['user']['id'];
        var url = AJAX_PATH + "associationcomment.ajax.php";
        var data = {action: "addcomment", id: $("#association").text(), id_user: id_user, comment: comment};

        if(comment != "") {
            if(!id_user) {
                tb_init_login("ajax", url, data);
            } else {
                $.ajax({type: "POST",
                       url: url,
                       data: data,
                       success: function(data){
                           $(".add_comment").val("");
                           $("#nbcomments").html(nbcomments+1);
                           $('#listing_comments').prepend(data);
                           $('#listing_comments').find(".comment:first").slideDown("fast");
                           $(".comment_delete").click(function(){
                                deleteComment($(this));
                           });
                       }
                });
            }
        } else {
            $("#error-com").html("Votre commentaire est vide");
            $("#error-com").slideDown("fast");
        }
    });

    $(".assoLeft").click(function() {
       var num_id = $(this).parents("div").parents("div").parents("div").attr("id");
       num = num_id.substr(3);
       $("#top"+num).fadeOut("slow", function(){
           num--;
           $("#top"+num).fadeIn("slow");

        });

    });

    $(".assoRight").click(function() {
       var num_id = $(this).parents("div").parents("div").parents("div").attr("id");
       num = num_id.substr(3);
       $("#top"+num).fadeOut("slow", function(){
           num++;
           $("#top"+num).fadeIn("slow");

        });

    });

    $(".comment_delete").click(function(){
        deleteComment($(this));
    });

	$(".pager a").attr("href", "#poster");
	$(".pager a").each(function() {
    var t = $(this);
	t.click(function() {
    
    var page=$(this).text( );
	$.ajax({ type: "POST",
                 url: AJAX_PATH+"associationcomment.ajax.php",
                 data:"action=pager&page="+page+"&id="+$("#association").text( ),
                 beforeSend:function(){
                     $('#listing_comments').fadeOut("fast");
                      $('#listing_comments').html("<img src='"+IMG_PATH+"preloader.gif' alt='Chargement...' />")
                 }, success:function(data){
                     $('.pageron').removeClass("pageron");
                     t.parent().addClass("pageron");
                     $('#listing_comments').html(data, function(){});
                     $('#listing_comments').fadeIn("slow");
                     $(".comment_delete").click(function(){
                        deleteComment($(this));
                     });
                 }});
		
	});
});
});

function deleteComment(t) {
     var nbcomments = parseInt($("#nbcomments").html());
        var tab_id_commentaire = t.parents("div").attr("id").split("_");
        id_commentaire = tab_id_commentaire[1];
        $.ajax({type: "POST",
                   url: AJAX_PATH+"associationcomment.ajax.php",
                   data:"action=delcomment&id="+$("#association_id").text()+"&id_commentaire="+id_commentaire,
                   success:function(){
                       $("#nbcomments").html(nbcomments-1);
                       t.parents(".comment").slideUp("fast");
                   }
            });
}

function trim (myString){
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
} 